package com.cola.custSpeechTranscription.cust;

import com.cola.custSpeechTranscription.api.CustShareSessionManager;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netease.lowcode.core.annotation.NaslLogic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: speechTranscription
 * @BelongsPackage: com.example.demo.cust
 * @Author: jiangkang
 * @CreateTime: 2025-10-29  11:12
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class CustChatConApi {
    @Autowired
    private CustShareSessionManager shareSessionManager;

    private static final Logger log = LoggerFactory.getLogger("LCAP_EXTENSION_LOGGER");

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @NaslLogic
    public CustMessageRsp sendSayMessage(CustMessageRequest request) {
        String userId = request.getUserId();
        String message = request.getMessageStr();

        log.info("收到保存文字消息，userid为：{}，消息为：{}",userId,message);
        CustMessageRsp messageRsp = new CustMessageRsp();
        if (userId == null || userId.trim().isEmpty()) {
            messageRsp.setMessageStr("用户ID不能为空");
            messageRsp.setStatus("error");
            return messageRsp;
        }

        // 添加到历史数据
        Map<String, Object> map = new HashMap<>();
        map.put("type","say");
        map.put("msg",message);
        try{
            String str = MAPPER.writeValueAsString(map);
            shareSessionManager.addTranscript(userId, str);
        }catch (Exception e){
            log.info("收到保存文字消息，userid为：{}，发送消息失败：{}",userId,e);
            messageRsp.setMessageStr("发送失败");
            messageRsp.setStatus("error");
            return messageRsp;
        }

        messageRsp.setStatus("success");
        messageRsp.setUserId(userId);
        messageRsp.setTimestamp(System.currentTimeMillis());
        return messageRsp;
    }

}